/**
* Javascipt Library for  dialog
* @Ray Li
* dependency
*/

//namespace declaration
function cms(){}
function cms.Dialog(){}

var application_url = location.protocol + "//" + location.host + "/ctitJbpm/XiorkFlow/";
/*
 *  A class for XML Remote Call
 */
function cms.Dialog.Ajax(url, func, args, isGet)
{
	this.url = url;
	this.xmlhttp = cms.Dialog.GetXmlHttp();
	
 	if (this.xmlhttp == null) {
  		//ShowAlertMessageDialog("Browser compatibility issue detected, please see a system administrator for assistance.");
		return;
	}
    var objxml = this.xmlhttp;
	this.xmlhttp.onreadystatechange = function(){cms.Dialog.Ajax.StateChangeHandler(objxml,func,args);};			
	if(isGet)
	{
		this.xmlhttp.open("GET", url , true);
		this.xmlhttp.send(null);		
	}
	else
	{
		this.xmlhttp.open("POST", api_url , true);
		this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 		
		this.xmlhttp.send(url);
	}
}

cms.Dialog.Ajax.StateChangeHandler = function(xmlhttp,func,args)
{
	if (xmlhttp.readyState == 4) 
	{
		if (xmlhttp.status == 200) {
			//Loaded();
			//Debug(xmlhttp.responseText);
			if(args)
			{
				switch(args.length)
				{
					case 0: func(xmlhttp); break;
					case 1: func(xmlhttp,args[0]);break;
					case 2: func(xmlhttp,args[0],args[1]);break;
				}			
			}
			else
			{
				func(xmlhttp);
			}			
		}
		else
		{
			//ShowAlertMessageDialog("Your session has timed out.  You will now be returned to the log in page.");
			//Loaded();
		}
	}
	else
	{
		//Loading();//show loading status logo
	}		
}

/*
 * Generate the XmlHttpRequest for different browses
 */
function cms.Dialog.GetXmlHttp()
{
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   //ShowAlertMessageDialog("XMLHttpRequest not supported");
   return null;
}

function cms.Dialog.LoadAlertMessageTemplate(url,panel,func)
{
	var args = func?[panel,func]:[panel];

	cms.Dialog.Ajax(url, cms.Dialog.ShowDialogTemplate, args, true);
}

/*
* show Dialog Template 
*/
function cms.Dialog.ShowDialogTemplate(xmlhttp,panelId,func)
{
	if($(panelId))
		$(panelId).innerHTML = xmlhttp.responseText;		

	if(func)
		func();
}